import {
	HTTP_REQUEST_URL_ZX,
	HEADER,
	TOKENNAME
} from '@/config/app_member.js';
import {
	toLogin,
	checkLogin
} from '../libs/login';
import store from '../store';


/**
 * 发送请求
 */
function baseRequest(urlType,url, method, data, {
	noAuth = false,
	noVerify = false
}) {
	let Url = HTTP_REQUEST_URL_ZX,
		header = HEADER;
	if (!noAuth) {
		//登录过期自动登录
		if (!store.state.app.token && !checkLogin()) {
			toLogin();
			return Promise.reject({
				msg: '未登录'
			});
		}
	}
	if (store.state.app.member_token){
		header[TOKENNAME] = store.state.app.member_token;
	} else{
		return;
	}
	if((url.indexOf("h5/messageReplySubmit") !== -1) || (url.indexOf("h5/modifyBankSubmit") !== -1) || (url.indexOf("h5/messageAddSubmit") !== -1) || (url.indexOf(("h5/modifyLoginPasswordSubmit")) !==-1)){
		header["Content-Type"]="application/x-www-form-urlencoded";
	}
	if(url.indexOf("h5/changeMemberInfoImage") !==-1){
		header["Content-Type"]="multipart/form-data";
	}
	return new Promise((reslove, reject) => {
		if(urlType=='shop_ht'){
			uni.request({
				url: url,
				method: method || 'GET',
				data: data || {},
				complete: (data) => {
					if (!uni.getStorageSync('SERVER_TYPE')) {
						// #ifdef H5 || APP-PLUS
						uni.setStorageSync('SERVER_TYPE', data.header.server);
						//  #endif
						// #ifdef MP
						uni.setStorageSync('SERVER_TYPE', data.header.Server);
						// #endif
					}
				},
				success: (res) => {
					if (noVerify)
						reslove(res.data, res);
					else if (res.data.meta.success == true)
						reslove(res.data, res);
					else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
						toLogin();
						reject(res.data);
					} else if (res.data.meta.success == false) {
						if(res.data.meta.message){
							uni.showModal({
								title: '提示',
								content: res.data.meta.message,
								showCancel: false,
								confirmText: '我知道了'
							});
						}
					} else
					  console.log("cccccc");
						reject(res.data.meta.mag || '系统错误');
				},
				fail: (msg) => {
					let data = {
						mag: '请求失败',
						status: 1 //1没网
					}
					// #ifdef APP-PLUS
					reject(data.mag);
					// #endif
					// #ifndef APP-PLUS
					reject('请求失败');
					// #endif
				}
			})
		}else{
			uni.request({
				url: url,
				method: method || 'GET',
				header: header,
				data: data || {},
				complete: (data) => {
					if (!uni.getStorageSync('SERVER_TYPE')) {
						// #ifdef H5 || APP-PLUS
						uni.setStorageSync('SERVER_TYPE', data.header.server);
						//  #endif
						// #ifdef MP
						uni.setStorageSync('SERVER_TYPE', data.header.Server);
						// #endif
					}
				},
				success: (res) => {
					if (noVerify)
						reslove(res.data, res);
					else if (res.data.meta.success == true)
						reslove(res.data, res);
					else if (res.data.meta.message =="Token无效!") {
						toLogin();
						reject(res.data);
					} else if (res.data.meta.success == false) {
						uni.showModal({
							title: '提示',
							content: res.data.meta.message,
							showCancel: false,
							confirmText: '我知道了'
						});
						reslove(res.data, res);
					} else
						reject(res.data || '系统错误');
				},
				fail: (msg) => {
					let data = {
						mag: '请求失败',
						status: 1 //1没网
					}
					// #ifdef APP-PLUS
					reject(data);
					// #endif
					// #ifndef APP-PLUS
					reject('请求失败');
					// #endif
				}
			})
		}
		
	});
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (urlType,api, data, opt) => baseRequest(urlType,api, method, data, opt || {})
});



export default request;
